package com.ldpp.disruptor.producer;

import cn.hutool.core.convert.Convert;
import com.ldpp.disruptor.event.Event;
import com.lmax.disruptor.RingBuffer;

import java.nio.ByteBuffer;

/**
 * @author ：gongtan
 * @date ：Created in 2021/3/15 10:20
 * @description： 生产者
 * @modified By：
 */
public class LdppEventProducer {

    private RingBuffer<Event> ringBuffer;

    //构造
    public LdppEventProducer(RingBuffer<Event> ringBuffer){
        this.ringBuffer = ringBuffer;
    }

    public void onData(Event event){
        //拿到环形的序列位置 (下标位置)
        long sequence = ringBuffer.next();
        try {
            //取出 空队列（Event）
            Event eventNull = ringBuffer.get(sequence);
            //给空队列项 赋值
            eventNull.setEventType(event.getEventType());
            eventNull.setEventDict(event.getEventDict());
        }catch (Exception e){
            // TODO ：handle Exception
        }finally {
            System.out.println("event生产者发送数据");
            //发送数据
            ringBuffer.publish(sequence);
        }

    }
}
